/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureDAO;
import cz.muni.fi.pa165.creatures.model.Creature;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.Creature} instances.
 *
 * @author xmiklos1
 */
@Transactional
@Repository
public class CreatureDAOImpl implements CreatureDAO {

    @PersistenceContext
    private EntityManager em;
    
    private static final Logger logger = 
            Logger.getLogger(CreatureDAOImpl.class.getName());    
    
    @Override
    public void create(Creature creature) {
        try {
            DAOUtils.notNull(creature);
            em.persist(creature);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to create a creature. "
                    + "Creature cannot be a null object.");
        } catch (EntityExistsException ex) {
            logger.log(Level.INFO, "Unable to create a creature. "
                    + "Creature to create is already created.");
        }
    }

    @Override
    public void remove(Creature creature) {
        try {
            DAOUtils.notNull(creature);
            Creature toRemove = em.find(Creature.class, creature.getId());
            em.remove(toRemove);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to remove a creature. "
                    + "Creature cannot be a null object.");
        }
    }
    
    @Override
    public void update(Creature creature) {
        try {
            DAOUtils.notNull(creature);
            em.merge(creature);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to update a creature. "
                    + "Creature cannot be a null object.");
        }
    }

    @Override
    public Creature get(Long id) {
        try {
            DAOUtils.notNull(id);
            Creature creature = em.find(Creature.class, id);
            return creature;
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to get a creature. "
                    + "Id of the creature cannot be a null object.");
            return null;
        }
    }

    @Override
    public int getCount() {
        Query q = em.createQuery("select count(o) from Creature as o");
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public List<Creature> getAll() {
        Query q = em.createQuery("from Creature");
        return q.getResultList();
    }
}
